﻿namespace Mytodotodaylist.WF.Client.Views.MainView
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using AddEditView;
    using Core.Container;
    using Core.Entites;
    using Extensions;

    public partial class MainFormImpl : Form, IMainView
    {
        private IAddEditView addEditView;
        public MainPresenter Presenter;

        public MainFormImpl()
        {
            InitializeComponent();

            dgvAllNotifs.AutoGenerateColumns = false;
            dgvActiveNotifs.AutoGenerateColumns = false;
            addEditView = IoC.Resolve<IAddEditView>();
        }

        #region IMainView Members

        public void Initialise(MainPresenter presenter)
        {
            Presenter = presenter;
        }

        public void ShowAllNotifications(List<Notification> evts)
        {
            this.InvokeEx(f => f.dgvAllNotifs.DataSource = evts);
        }

        public void ShowActiveNotifications(List<Notification> evts)
        {
            this.InvokeEx(f => f.dgvActiveNotifs.DataSource = evts);
        }

        #endregion

        private void btnAdd_Click(object sender, EventArgs e)
        {
            addEditView.SetModel(null);
            if (addEditView.ShowDialog() == DialogResult.OK)
                Presenter.AddEvent(addEditView.GetModel());
        }

        private void dgvAllNotifs_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var not = (Notification) dgvAllNotifs.CurrentRow.DataBoundItem;

            if (not != null)
            {
                addEditView.SetModel(not.TheEvent);
                if (addEditView.ShowDialog() == DialogResult.OK)
                    Presenter.UpdateEvent(addEditView.GetModel());
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var not = (Notification) dgvAllNotifs.CurrentRow.DataBoundItem;
            if ((not != null) && 
                (MessageBox.Show("Are you sure you want to delete selected event?") == DialogResult.OK))
            {
                Presenter.DeleteEvent(not.TheEvent.Id);
            }
        }
    }
}